package view;
import events.ResizeEvent;
import flash.display.DisplayObject;
import flash.display.LineScaleMode;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.Lib;
import graphic.RenderObject;
import graphic.Scene;
import gui.menu.MainMenuItem;
import gui.menu.MenuItemType;
import gui.menu.MenuBar;
import flash.display.Sprite;
import gui.menu.MenuItem;
import model.LevelEditorModel;
import model.LevelEditorModelEvent;
import model.ModelUpdateType;
import phx.Body;
import phx.FlashDraw;
import phx.Shape;
import phx.Vector;
import view.events.LevelEditorViewKeyboardEvent;
import view.events.LevelEditorViewMouseEvent;

/**
 * @author Damien Mabin
 */

class LevelEditorView extends Sprite
{

	private var m_Menu 				: MenuBar;
	private var m_LoadLevelItem 	: MenuItem;
	private var m_SaveLevelItem 	: MenuItem;
	private var m_Model				: LevelEditorModel;
	private var m_InternalScene		: Scene;
	private var m_Correspondance	: Array<{ modelRO : RenderObject, internalRO : RenderObject }>;
	private var m_AskedSize			: Point;
	private var m_LastDragPosition	: Point;
	private var m_CreateROPanel		: CreateRenderObjectPanel;
	private var m_PropertyPanel		: PropertyPanel;
	private var m_ActionBar			: ActionBar;
	private var m_PhysicPoints		: Array<{ internalPoint : PhysicPoint, modelPoint : Vector }>;
	
	private var m_PhysWorldDisplay	: Sprite;
	private var m_PhysWorldDrawer	: FlashDraw;
	
	// Skin :
	static var s_MenuStyle = { backgroundColor : 0xBFCC85, backgroundAlpha : 1.0, borderColor : 0x748F74, borderAlpha : 1.0, foregroundColor : 0x88FFED9C, foregroundAlpha : 0.15};
	static var s_LevelEditorStyle = { backgroundColor : 0x454545 };
	static var s_RenderObjectSelectedFilter : GlowFilter = new GlowFilter(0xD9B500, 0.8, 10, 10, 5, 5, false);
	static var s_PhysPointSelectedFilter 	: GlowFilter = new GlowFilter(0x454545, 0.8, 10, 10, 5, 5, false);
	static var s_CreateROPanelStyle = { backgroundColor : 0x748F74, separatorColor : 0x454545, hightlightColor : 0xD9B500 };
	static var s_PropertyPanelStyle = { backgroundColor : 0x748F74, separatorColor : 0x454545, buttonBackgroundColor : 0xBFCC85, buttonForegroundColor : 0x88FFED9C };
	static var s_ActionBarStyle = { backgroundColor : 0x748F74, buttonBackgroundColor : 0xBFCC85, buttonForegroundColor : 0x88FFED9C };
	
	public function new(_Model:LevelEditorModel) 
	{
		super();
		m_Model = _Model;
		m_AskedSize = new Point();
		m_LastDragPosition = new Point();
		m_Correspondance = new Array<{ modelRO : RenderObject, internalRO : RenderObject }>();
		m_PhysicPoints = new Array<{ internalPoint : PhysicPoint, modelPoint : Vector }>();
		m_PhysWorldDisplay = new Sprite();
		m_PhysWorldDrawer = new FlashDraw(m_PhysWorldDisplay.graphics);
		m_PhysWorldDrawer.drawCircleRotation = true;
		m_ActionBar = new ActionBar(m_Model, s_ActionBarStyle);
		
		// ************************************************************************************************
		// Top menu bar
		m_Menu = new MenuBar(s_MenuStyle);
		MainMenuItem.PADDINGSIZE = 0;
		
		// ------------------------------------------------
		// File menu :
		var fileTree : MainMenuItem = new MainMenuItem();
		
			// New project
			m_LoadLevelItem = new MenuItem(MenuItemType.CLASSIC, "Load level");
			m_LoadLevelItem.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseEvent);
			fileTree.AddItem(m_LoadLevelItem);
			
			// Save project
			m_SaveLevelItem = new MenuItem(MenuItemType.CLASSIC, "Save level");
			m_SaveLevelItem.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseEvent);
			fileTree.AddItem(m_SaveLevelItem);
			
		fileTree.m_Style = s_MenuStyle;
		fileTree.m_IsExtendableArrow = MainMenuItem.getDefaultIsExtendableArrow();
		fileTree.UpdateStyle();
		m_Menu.AddMenu("File", fileTree);
		
		// ------------------------------------------------
		
		// ************************************************************************************************
		// Create render object panel : 
		m_CreateROPanel = new CreateRenderObjectPanel(s_CreateROPanelStyle);
		
		// ************************************************************************************************
		// Property panel : Display the properties of selected element :
		m_PropertyPanel = new PropertyPanel(s_PropertyPanelStyle);
		
		// ************************************************************************************************
		LoadModel();
		
		this.addChild(m_InternalScene);
		this.addChild(m_CreateROPanel);
		this.addChild(m_PropertyPanel);
		this.addChild(m_ActionBar);
		this.addChild(m_Menu);
		
		this.addEventListener(ResizeEvent.EVENT, OnResize);
		Lib.current.stage.addEventListener(Event.ENTER_FRAME, OnEnterFrameEvent);
		Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyboardEvent);
		Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, OnKeyboardEvent);
		m_Model.addEventListener(LevelEditorModelEvent.EVENT, OnModelUpdate);
		this.addEventListener(LevelEditorModelEvent.EVENT, OnModelUpdate);
	}
	
	private function OnKeyboardEvent(_Evt:KeyboardEvent):Void
	{
		switch(_Evt.type)
		{
			case KeyboardEvent.KEY_UP :
			{
				this.dispatchEvent(cast new LevelEditorViewKeyboardEvent(E_KeyboardInteractionType.KEY_UP, _Evt.keyCode));
			}
			case KeyboardEvent.KEY_DOWN :
			{
				this.dispatchEvent(cast new LevelEditorViewKeyboardEvent(E_KeyboardInteractionType.KEY_DOWN, _Evt.keyCode));
			}
		}
	}
	
	private function OnEnterFrameEvent(_Evt:Event):Void
	{
		m_InternalScene.Update(Std.int(1000/Lib.current.stage.frameRate));
	}
	
	private function OnModelUpdate(_Evt:LevelEditorModelEvent)
	{
		#if debug
			trace("View Update : " + _Evt.m_Type);
		#end
		switch(_Evt.m_Type)
		{
			case RELOAD_MODEL :
			{
				LoadModel();
			}
			case ADD_RENDER_OBJECT_TO_SELECTION(ro) :
			{
				for (corres in m_Correspondance)
				{
					if (corres.modelRO == ro)
					{
						corres.internalRO.filters = [s_RenderObjectSelectedFilter];
						break;
					}
				}
				if (m_Model.VUE_GetSelection(RenderObject).length == 1)
				{
					m_PropertyPanel.DisplayProperties(RenderObject, ro);
				}
				else
				{
					m_PropertyPanel.DisplayProperties(null, null);
				}
			}
			case ADD_PHYSIC_POINT_TO_SELECTION(pt) :
			{
				for (physPt in m_PhysicPoints)
				{
					if (physPt.modelPoint == pt)
					{
						physPt.internalPoint.filters = [s_PhysPointSelectedFilter];
					}
				}
			}
			case ADD_NEW_RENDER_OBJECT(ro) :
			{
				var internalRO : RenderObject = ro.Clone();
				m_Correspondance.push( { modelRO : ro, internalRO : internalRO } );
				m_InternalScene.AddRenderObject(internalRO);
				internalRO.addEventListener(MouseEvent.MOUSE_DOWN, OnRenderObjectMouseEvent);
				RedrawPhysicWorld();
				OnResize(new ResizeEvent(m_AskedSize.x, m_AskedSize.y));
			}
			case ADD_PHYSIC_POINT(previousPt, newPt) :
			{
				trace("TODO : Update view !");
				var index : Int = 0;
				for (corres in m_PhysicPoints)
				{
					if (corres.modelPoint == previousPt)
					{
						var internalPoint : PhysicPoint = new PhysicPoint();
						internalPoint.x = newPt.x;
						internalPoint.y = newPt.y;
						internalPoint.scaleX = 0.2;
						internalPoint.scaleY = 0.2;
						internalPoint.addEventListener(MouseEvent.MOUSE_DOWN, OnPhysicPointMouseEvent);
						m_PhysicPoints.insert(index, { modelPoint : newPt, internalPoint : internalPoint });
						var parent : Sprite = cast corres.internalPoint.parent;
						parent.addChildAt(internalPoint, index + 1);
						parent.graphics.clear();
						parent.graphics.lineStyle(1, 0x454545, 2.0, false, LineScaleMode.NONE);
						if (parent.numChildren > 1)
						{
							parent.graphics.moveTo(parent.getChildAt(1).x, parent.getChildAt(1).y);
							for (i in 1 ... parent.numChildren)
							{
								parent.graphics.lineTo(parent.getChildAt(i).x, parent.getChildAt(i).y);
							}
							parent.graphics.lineTo(parent.getChildAt(1).x, parent.getChildAt(1).y);
						}
						RedrawPhysicWorld();
						break;
					}
					index++;
				}
			}
			case REMOVE_RENDER_OBJECT_FROM_SELECTION(ro) :
			{
				for (corres in m_Correspondance)
				{
					if (corres.modelRO == ro)
					{
						corres.internalRO.filters = [];
						break;
					}
				}
				if (m_Model.VUE_GetSelection(RenderObject).length == 1)
				{
					m_PropertyPanel.DisplayProperties(RenderObject, ro);
				}
				else
				{
					m_PropertyPanel.DisplayProperties(null, null);
				}
			}
			case REMOVE_PHYSIC_POINT_FROM_SELECTION(pt) :
			{
				for (corres in m_PhysicPoints)
				{
					if (corres.modelPoint == pt)
					{
						corres.internalPoint.filters = [];
						break;
					}
				}
			}
			case MOVE_VIEW_POINT(pt) :
			{
				m_InternalScene.SetViewPoint(pt);
			}
			case MOVE_RENDER_OBJECT(ro) :
			{
				for (corres in m_Correspondance)
				{
					if (corres.modelRO == ro)
					{
						var p : Point = ro.GetPosition();
						corres.internalRO.SetPosition(p.x, p.y);
						var body = corres.internalRO.GetPhysicalBody();
						if (body != null)
						{
							m_InternalScene.GetWorld().sync(corres.internalRO.GetPhysicalBody());
						}
						RedrawPhysicWorld();
						break;
					}
				}
				if (m_Model.VUE_GetSelection(RenderObject).length == 1)
				{
					m_PropertyPanel.DisplayProperties(RenderObject, ro);
				}
				else
				{
					m_PropertyPanel.DisplayProperties(null, null);
				}
			}
			case MOVE_PHYSIC_POINT(pt) :
			{
				for (corres in m_PhysicPoints)
				{
					if (corres.modelPoint == pt)
					{
						corres.internalPoint.x = pt.x;
						corres.internalPoint.y = pt.y;
						
						var parent : Sprite = cast corres.internalPoint.parent;
						parent.graphics.clear();
						parent.graphics.lineStyle(1, 0x454545, 2.0, false, LineScaleMode.NONE);
						if (parent.numChildren > 1)
						{
							parent.graphics.moveTo(parent.getChildAt(1).x, parent.getChildAt(1).y);
							for (i in 1 ... parent.numChildren)
							{
								parent.graphics.lineTo(parent.getChildAt(i).x, parent.getChildAt(i).y);
							}
							parent.graphics.lineTo(parent.getChildAt(1).x, parent.getChildAt(1).y);
						}
						RedrawPhysicWorld();
						break;
					}
				}
			}
			case DELETE_RENDER_OBJECT(ro) :
			{
				for (corres in m_Correspondance)
				{
					if (corres.modelRO == ro)
					{
						m_InternalScene.RemoveRenderObject(corres.internalRO);
						m_Correspondance.remove(corres);
						RedrawPhysicWorld();
						break;
					}
				}
			}
			case PREPARE_RENDER_OBJECT_CREATION(name, file) :
			{
				m_CreateROPanel.SetSelectedRenderObject(name, file);
			}
			case UPDATE_RENDER_OBJECT_PROPERTIES(ro) :
			{
				for (corres in m_Correspondance)
				{
					if (corres.modelRO == ro)
					{
						corres.internalRO.name = ro.name;
						corres.internalRO.SetPosition(ro.GetPosition().x, ro.GetPosition().y);
						corres.internalRO.z = ro.z;
						corres.internalRO.scaleX = ro.scaleX;
						corres.internalRO.scaleY = ro.scaleY;
						corres.internalRO.rotation = ro.rotation;
						
						var internalBody:Body = corres.internalRO.GetPhysicalBody();
						var modelBody:Body = corres.modelRO.GetPhysicalBody();
						internalBody.isStatic == modelBody.isStatic;
						internalBody.setPos(modelBody.x, modelBody.y, modelBody.a);
						m_InternalScene.GetWorld().sync(internalBody);
						RedrawPhysicWorld();
						break;
					}
				}
				if (m_Model.VUE_GetSelection(RenderObject).length == 1)
				{
					m_PropertyPanel.DisplayProperties(RenderObject, m_Model.VUE_GetSelection(RenderObject)[0]);
				}
				else
				{
					m_PropertyPanel.DisplayProperties(null, null);
				}
			}
			case PREPARE_RENDER_OBJECT_PHYSIC_SHAPE_EDITION(ro) :
			{
				for (corres in m_Correspondance)
				{
					if (corres.modelRO == ro)
					{
						EnablePhysicShapeEdit(corres.internalRO);
						m_PropertyPanel.DisplayPropertiesForPhysics(RenderObject, ro);
					}
					else
					{
						corres.internalRO.alpha = 0.3;
					}
				}
				
			}
			case QUITE_RENDER_OBJECT_PHYSIC_SHAPE_EDITION(ro) :
			{
				if (m_PhysicPoints[0] != null)
				{
					m_InternalScene.m_SceneContent.removeChild(m_PhysicPoints[0].internalPoint.parent);
					while(m_PhysicPoints.length > 0)
					{
						m_PhysicPoints.pop();
					}
					m_PropertyPanel.DisplayProperties(null, null);
				}
				for (corres in m_Correspondance)
				{
					corres.internalRO.alpha = 1.0;
				}
				
				for (corres in m_Correspondance)
				{
					if (corres.modelRO == ro)
					{
						m_InternalScene.RemoveRenderObject(corres.internalRO);
						m_Correspondance.remove(corres);
						break;
					}
				}
				var internalRO : RenderObject = ro.Clone();
				m_Correspondance.push( { modelRO : ro, internalRO : internalRO } );
				m_InternalScene.AddRenderObject(internalRO);
				internalRO.addEventListener(MouseEvent.MOUSE_DOWN, OnRenderObjectMouseEvent);
				RedrawPhysicWorld();
				OnResize(new ResizeEvent(m_AskedSize.x, m_AskedSize.y));
			}
			case CHANGE_ZOOM(zoom):
			{
				m_InternalScene.SetZoom(zoom);
			}
		}
	}
	
	private function EnablePhysicShapeEdit(_ModelRO:RenderObject):Void
	{
		for (shape in _ModelRO.GetPhysicalBody().shapes.iterator())
		{
			switch(shape.type)
			{
				case Shape.POLYGON :
				{
					var shapeBackground:Sprite = new Sprite();
					var internalCenter : CenterPoint = new CenterPoint();
					internalCenter.scaleX = 0.3;
					internalCenter.scaleY = 0.3;
					internalCenter.x = 0;
					internalCenter.y = 0;
					shapeBackground.addChild(internalCenter);
					var ite:Vector = shape.polygon.verts;
					shapeBackground.graphics.moveTo(ite.x, ite.y);
					while (ite != null)
					{
						var polyPoint : PhysicPoint = new PhysicPoint();
						polyPoint.addEventListener(MouseEvent.MOUSE_DOWN, OnPhysicPointMouseEvent);
						polyPoint.scaleX = 0.2;
						polyPoint.scaleY = 0.2;
						polyPoint.x = ite.x;
						polyPoint.y = ite.y;
						shapeBackground.addChild(polyPoint);
						m_PhysicPoints.push({modelPoint : ite, internalPoint : polyPoint});
						
						shapeBackground.graphics.lineStyle(1, 0x454545, 2.0, false, LineScaleMode.NONE);
						shapeBackground.graphics.lineTo(ite.x, ite.y);
						
						ite = ite.next;
					}
					shapeBackground.graphics.lineTo(shape.polygon.verts.x, shape.polygon.verts.y);
					
					shapeBackground.x = _ModelRO.x;
					shapeBackground.y = _ModelRO.y;
					m_InternalScene.m_SceneContent.addChild(shapeBackground);
				}
			}
		}
	}
	
	/**
	 * Erase all what we have previously in the view, and load the model (var m_Model)
	 */
	public function LoadModel():Void
	{
		if (m_InternalScene != null)
		{
			this.removeChild(m_InternalScene);
			m_Correspondance = new Array<{ modelRO : RenderObject, internalRO : RenderObject }>();
		}
		m_InternalScene = new Scene();
		m_InternalScene.GetWorld().gravity = new Vector(0, 0);
		m_InternalScene.SetViewPoint(m_Model.VUE_GetViewPoint());
		m_InternalScene.SetZoom(m_Model.VUE_GetZoomLevel());
		m_InternalScene.m_PlayPhysic = false;
		this.addChild(m_InternalScene);
		m_InternalScene.m_SceneContent.addChild(m_PhysWorldDisplay);
		
		for (ro in m_Model.VUE_GetRenderObjects())
		{
			var internalRO : RenderObject = ro.Clone();
			m_Correspondance.push( { modelRO : ro, internalRO : internalRO } );
			m_InternalScene.AddRenderObject(internalRO);
			internalRO.addEventListener(MouseEvent.MOUSE_DOWN, OnRenderObjectMouseEvent);
		}
		RedrawPhysicWorld();
		
		m_InternalScene.addEventListener(MouseEvent.MOUSE_DOWN, OnSceneMouseEvent);
		m_InternalScene.addEventListener(MouseEvent.MOUSE_UP, OnSceneMouseEvent);
		m_InternalScene.addEventListener(MouseEvent.MOUSE_WHEEL, OnSceneMouseEvent);
		
		OnResize(new ResizeEvent(m_AskedSize.x, m_AskedSize.y));
	}
	
	private function RedrawPhysicWorld()
	{
		m_PhysWorldDisplay.graphics.clear();
		m_PhysWorldDrawer.drawWorld(m_InternalScene.GetWorld());
	}
	
	private function OnPhysicPointMouseEvent(_Evt:MouseEvent):Void
	{
		var modelPt : Vector = null;
		for (physicPt in m_PhysicPoints)
		{
			if (physicPt.internalPoint == _Evt.currentTarget)
			{
				modelPt = physicPt.modelPoint;
				break;
			}
		}
		if (modelPt != null)
		{
			switch(_Evt.type)
			{
				case MouseEvent.MOUSE_DOWN :
				{
					m_LastDragPosition.x = _Evt.stageX;
					m_LastDragPosition.y = _Evt.stageY;
					var tempoPoint : Point = new Point(_Evt.stageX, _Evt.stageY);
					var evt : LevelEditorViewMouseEvent = new LevelEditorViewMouseEvent(E_MouseInteractionType.MOUSE_DOWN, E_TargetType.PHYSIC_POINT, m_InternalScene.globalToLocal(tempoPoint));
					evt.m_Target = modelPt;
					this.dispatchEvent(evt);
					m_InternalScene.addEventListener(MouseEvent.MOUSE_MOVE, OnSceneMouseEvent);
				}
				default :
				{
					
				}
			}
		}
	}
	
	private function OnRenderObjectMouseEvent(_Evt:MouseEvent):Void
	{
		// On cherche sur quel element de la scene du modele on a cliqué
		var modelRO : RenderObject = null;
		for (corres in m_Correspondance)
		{
			if (corres.internalRO == _Evt.target)
			{
				modelRO = corres.modelRO;
				break;
			}
		}
		
		if (modelRO != null)
		{
			switch(_Evt.type)
			{
				case MouseEvent.MOUSE_DOWN :
				{
					m_LastDragPosition.x = _Evt.stageX;
					m_LastDragPosition.y = _Evt.stageY;
					var evt : LevelEditorViewMouseEvent = new LevelEditorViewMouseEvent(E_MouseInteractionType.MOUSE_DOWN, E_TargetType.RENDER_OBJECT, m_InternalScene.globalToLocal(m_LastDragPosition));
					evt.m_Target = modelRO;
					this.dispatchEvent(evt);
					m_InternalScene.addEventListener(MouseEvent.MOUSE_MOVE, OnSceneMouseEvent);
				}
				default :
				{
					
				}
			}
		}
	}
	
	private function OnSceneMouseEvent(_Evt:MouseEvent):Void
	{
		switch(_Evt.type)
		{
			case MouseEvent.MOUSE_DOWN :
			{
				if (_Evt.target == m_InternalScene)
				{
					m_LastDragPosition.x = _Evt.stageX;
					m_LastDragPosition.y = _Evt.stageY;
					var evt : LevelEditorViewMouseEvent = new LevelEditorViewMouseEvent(E_MouseInteractionType.MOUSE_DOWN, E_TargetType.SCENE, m_InternalScene.globalToLocal(m_LastDragPosition));
					evt.m_Target = m_InternalScene;
					this.dispatchEvent(cast evt);
					m_InternalScene.addEventListener(MouseEvent.MOUSE_MOVE, OnSceneMouseEvent);
				}
			}
			case MouseEvent.MOUSE_MOVE :
			{
				var tempoPoint : Point = new Point(_Evt.stageX, _Evt.stageY);
				var evt : LevelEditorViewMouseEvent = new LevelEditorViewMouseEvent(E_MouseInteractionType.MOUSE_MOVE, E_TargetType.SCENE, m_InternalScene.globalToLocal(tempoPoint));
				evt.m_Target = m_InternalScene;
				evt.m_MouseMovement.x = tempoPoint.x - m_LastDragPosition.x;
				evt.m_MouseMovement.y = tempoPoint.y - m_LastDragPosition.y;
				this.dispatchEvent(cast evt);
				m_LastDragPosition = tempoPoint;
			}
			case MouseEvent.MOUSE_UP :
			{
				var evt : LevelEditorViewMouseEvent = new LevelEditorViewMouseEvent(E_MouseInteractionType.MOUSE_UP, E_TargetType.SCENE, m_InternalScene.globalToLocal(new Point(_Evt.stageX, _Evt.stageY)));
				evt.m_Target = m_InternalScene;
				this.dispatchEvent(cast evt);
				m_InternalScene.removeEventListener(MouseEvent.MOUSE_MOVE, OnSceneMouseEvent);
			}
			case MouseEvent.MOUSE_WHEEL :
			{
				var evt : LevelEditorViewMouseEvent = new LevelEditorViewMouseEvent(E_MouseInteractionType.MOUSE_WHEEL, E_TargetType.SCENE, m_InternalScene.globalToLocal(new Point(_Evt.stageX, _Evt.stageY)));
				evt.m_Target = m_InternalScene;
				evt.m_DeltaWheel = _Evt.delta;
				this.dispatchEvent(cast evt);
			}
		}
	}
	
	private function OnMouseEvent(_Evt:MouseEvent):Void
	{
		switch(_Evt.type)
		{
			case MouseEvent.MOUSE_DOWN :
			{
				if (_Evt.currentTarget == m_LoadLevelItem)
				{
					m_Menu.CollapseAll();
					m_Model.CTRL_LoadNewScene();
				}
				else if (_Evt.currentTarget == m_SaveLevelItem)
				{
					m_Menu.CollapseAll();
					m_Model.CTRL_SaveScene();
				}
			}
			default :
			{
				
			}
		}
	}
	
	private function OnResize(_Evt : ResizeEvent) : Void
	{
		m_AskedSize.x = _Evt.m_Width;
		m_AskedSize.y = _Evt.m_Height;
		
		this.graphics.beginFill(s_LevelEditorStyle.backgroundColor);
		this.graphics.drawRect(0, 0, _Evt.m_Width, _Evt.m_Height);
		
		var margin : Float = 5;
		
		m_Menu.dispatchEvent(new ResizeEvent(_Evt.m_Width, 30));
		m_Menu.x = 0;
		m_Menu.y = 0;
		
		m_CreateROPanel.x = margin;
		m_CreateROPanel.y = 30 + margin;
		m_CreateROPanel.dispatchEvent(new ResizeEvent(_Evt.m_Width * 0.25, (_Evt.m_Height - 30) * 0.5));
		
		m_PropertyPanel.x = m_CreateROPanel.x;
		m_PropertyPanel.y = m_CreateROPanel.y + m_CreateROPanel.height + margin;
		m_PropertyPanel.dispatchEvent(new ResizeEvent(_Evt.m_Width * 0.25, _Evt.m_Height - 30 - 2 * margin - 50 - margin - m_CreateROPanel.height-margin));
		
		m_InternalScene.x = m_CreateROPanel.x + _Evt.m_Width * 0.25 + margin;
		m_InternalScene.y = 30 + margin;
		m_InternalScene.dispatchEvent(new ResizeEvent(_Evt.m_Width * 0.6 - margin, _Evt.m_Height - 30 - 2 * margin - 50 - margin));
		
		m_ActionBar.x = m_InternalScene.x + _Evt.m_Width * 0.6;
		m_ActionBar.y = m_InternalScene.y;
		m_ActionBar.dispatchEvent(new ResizeEvent(_Evt.m_Width - m_ActionBar.x - margin, _Evt.m_Height - 30 - 2 * margin - 50 - margin));
		
	}
	
}